﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FHost1
{
    public partial class FormDefault : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString.AllKeys.Contains("login"))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Session expired. Please login!!');", true);
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            try
            {
                using (FHostDataContext dataContext = new FHostDataContext())
                {
                    int alreadyCount = dataContext.UserInfos.Where(obj => (obj.Username == txtUsername.Text.ToLowerInvariant())).Count();
                    if (alreadyCount == 0)
                    {
                        UserInfo userInfo = new UserInfo()
                        {
                            AccountType = 'F',
                            CreatedDate = DateTime.Now,
                            EmailID = txtEmailID.Text,
                            FullName = txtUsername.Text,
                            Password = txtPassword.Text,
                            UploadedSize = 0M,
                            Username = txtUsername.Text
                        };

                        dataContext.UserInfos.InsertOnSubmit(userInfo);
                        dataContext.SubmitChanges();

                        Folder folder = new Folder()
                        {
                            CreatedDate = DateTime.Now,
                            FolderName = "Main",
                            ParentFolderID = null,
                            UserID = userInfo.UserID
                        };

                        dataContext.Folders.InsertOnSubmit(folder);
                        dataContext.SubmitChanges();

                        txtEmailID.Text = "";
                        txtUsername.Text = "";

                        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Account created successfully. Please login!');", true);
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Username already exists. Please select different username!');", true);
                    }
                }
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Error creating account. Please try again after some time!');", true);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("Search.aspx?search={0}&page={1}&type={2}", txtSearch.Text, 1, ddlSearch.SelectedValue));
        }
    }
}